package com.example.addressdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.addressdemo.Address.Contact;
import com.example.addressdemo.DB.DBAdapter;
import com.example.addressdemo.View.LetterView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private DBAdapter dbAdapter;
    private RecyclerView rvMain;
    private com.example.addressdemo.View.IndexWord iwMain;
    private TextView tvMain;
    private Handler handler = new Handler();
    // private ArrayList<Contact> contacts=new ArrayList<>();;
    private com.example.addressdemo.LinkAdapter linkadapter;
    private int childCount;
    private LinearLayoutManager linearmanger;
    private int childCount1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("MainActivity", "onCreate");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        Button search_button = (Button) findViewById(R.id.search_button);

        dbAdapter = new DBAdapter(this);
        dbAdapter.open();
        setSupportActionBar(toolbar);

        final List<String> list = new ArrayList<String>();
        final ListView listView = (ListView) findViewById(R.id.contactlist);
        final LetterView letterView = (LetterView) findViewById(R.id.letterView);
        final TextView tvToast = (TextView) findViewById(R.id.show_now_abc);

        //初始化数据
        final Contact[] contactss = dbAdapter.getAllData();

        letterView.setOnLetterChangeListener(new LetterView.OnLetterChangeListener() {
            @Override
            public void onLetterChange(int selectedIndex) {
                tvToast.setText(LetterView.letters.charAt(selectedIndex) + "");//设置中间显示的字母
                tvToast.setVisibility(View.VISIBLE);//设置为可见
            }

            @Override
            public void onClickUp() {
                tvToast.setVisibility(View.GONE);//当放开时，设置为不可见
            }
        });


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);

        AdapterView.OnItemClickListener listViewListener = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {

                Bundle bundleSimple = new Bundle();
                bundleSimple.putString("name", contactss[position].name);
                bundleSimple.putString("phone", contactss[position].phone);
                bundleSimple.putString("work", contactss[position].work);
                bundleSimple.putString("home", contactss[position].home);

                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                intent.putExtras(bundleSimple);
                startActivity(intent);

            }
        };
        listView.setOnItemClickListener(listViewListener);


        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("type", "add");
                Intent intent = new Intent(MainActivity.this, AddActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}